package netty;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class NioServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("接受到客户端的消息" + "["+msg+"]");
        String s = msg.toString();
        if (s.startsWith("helloService#hello#")) {
            String substring = s.substring(s.lastIndexOf("#") + 1);
            ctx.writeAndFlush(substring);
            System.out.println("服务器发送给客户端消息"+substring);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}
